


--use PolePosition

-- // checksum failed custom error
--exec sp_addmessage @msgnum = 505050, @severity = 16, @msgtext = N'%s(%s) checksum compare failed', @replace = 'REPLACE', @lang = 'us_english'
GO

-- // not found custom error
--exec sp_addmessage @msgnum = 505060, @severity = 16, @msgtext = N'%s(%s) does not exist', @replace = 'REPLACE', @lang = 'us_english'
GO

-- // unit test custom error
--exec sp_addmessage @msgnum = 505070, @severity = 16, @msgtext = N'%s Test Failed. Expected< %s > Actual< %s > %s', @replace = 'REPLACE', @lang = 'us_english'
GO



--use PolePosition

if exists (select * from dbo.sysobjects where id = object_id(N'v_tb_Vendor_Settings') and OBJECTPROPERTY(id, N'IsProcedure') = 0)
drop view v_tb_Vendor_Settings
GO

create view v_tb_Vendor_Settings as
	/* 	Initial Generated Date:  Saturday, February 07, 2009 
		Purpose: Selected record(s) from the table; this view is the source for all procedures' select statements
	*/
	
select
	 [Vendor_SettingId] as [Vendor_SettingId]
	,[VendorFK] as [VendorFK]
	,[Value] as [Value]
	-- customization may be required for FriendlyName
	,Left(CONVERT(varchar(255), [Vendor_SettingId]), 255) as [FriendlyName]
from
	tb_Vendor_Settings
GO





--use PolePosition

if exists (select * from dbo.sysobjects where id = object_id(N'up_VendorSetting_Test') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure up_VendorSetting_Test
GO


create proc up_VendorSetting_Test
	@Vendor_SettingId int
as
	/* 	Initial Generated Date:  Saturday, February 07, 2009 
		Purpose: Test if a record exists, using the primary key
	*/

SET NOCOUNT ON	
	
if exists(
	select [Vendor_SettingId] 
	from [tb_Vendor_Settings] source
	where [source].[Vendor_SettingId] = @Vendor_SettingId)
begin
	return(1)
end
else
begin
	return(0)
end
GO






--use PolePosition

if exists (select * from dbo.sysobjects where id = object_id(N'up_VendorSetting_Select') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure up_VendorSetting_Select
GO

create proc up_VendorSetting_Select
	 @Vendor_SettingId int = null 
	,@VendorFK int = null 
	,@Value text = null 
as
	/* 	Initial Generated Date:  Saturday, February 07, 2009 
		Purpose: Selected record(s) based on the passed arguments; no arguments returns all
	*/

SET NOCOUNT ON	

declare @table table
	(
	 Vendor_SettingId int
	,VendorFK int
	,Value text
	,[FriendlyName] varchar(255)
	)
	
insert @table
	(
	 [Vendor_SettingId]
	,[VendorFK]
	,[Value]
	,[FriendlyName]
	)
select 
	 [Vendor_SettingId]
	,[VendorFK]
	,[Value]
	,[FriendlyName]
from v_tb_Vendor_Settings
where
		IsNull([v_tb_Vendor_Settings].[Vendor_SettingId], -1) = Coalesce(@Vendor_SettingId, [v_tb_Vendor_Settings].[Vendor_SettingId], -1)
	AND	IsNull([v_tb_Vendor_Settings].[VendorFK], -1) = Coalesce(@VendorFK, [v_tb_Vendor_Settings].[VendorFK], -1)
	AND	IsNull(Convert(varchar(2000), [v_tb_Vendor_Settings].[Value]), '') = Coalesce(Convert(varchar(2000), @Value), Convert(varchar(2000), [v_tb_Vendor_Settings].[Value]), '')

-- primary select
select * from @table





	
	-- VendorEntity as Parent select
	select
	v_tb_Vendors.* 
from 
	v_tb_Vendors 
	JOIN @table x ON v_tb_Vendors.VendorId = x.VendorFK
	

	
GO 





--use PolePosition

if exists (select * from dbo.sysobjects where id = object_id(N'up_VendorSetting_Select_Top1') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure up_VendorSetting_Select_Top1
GO

create proc up_VendorSetting_Select_Top1
	 @Vendor_SettingId int = null 
	,@VendorFK int = null 
	,@Value text = null 
as
	/* 	Initial Generated Date:  Saturday, February 07, 2009 
		Purpose: Selected record(s) based on the passed arguments; no arguments returns all
	*/

SET NOCOUNT ON	

declare @table table
	(
	  Vendor_SettingId int
	, VendorFK int
	, Value text
	,[FriendlyName] varchar(255)
	)

insert @table
	(
	 [Vendor_SettingId]
	,[VendorFK]
	,[Value]
	,[FriendlyName]
	)
select TOP 1
	 [Vendor_SettingId]
	,[VendorFK]
	,[Value]
	,[FriendlyName]
from v_tb_Vendor_Settings
where
		IsNull([v_tb_Vendor_Settings].[Vendor_SettingId], -1) = Coalesce(@Vendor_SettingId, [v_tb_Vendor_Settings].[Vendor_SettingId], -1)
	AND	IsNull([v_tb_Vendor_Settings].[VendorFK], -1) = Coalesce(@VendorFK, [v_tb_Vendor_Settings].[VendorFK], -1)
	AND	IsNull(Convert(varchar(2000), [v_tb_Vendor_Settings].[Value]), '') = Coalesce(Convert(varchar(2000), @Value), Convert(varchar(2000), [v_tb_Vendor_Settings].[Value]), '')

-- primary select
select * from @table


	
	-- VendorEntity as Parent select
	select
	v_tb_Vendors.* 
from 
	v_tb_Vendors 
	JOIN @table x ON v_tb_Vendors.VendorId = x.VendorFK
	
	
	
GO 





--use PolePosition

if exists (select * from dbo.sysobjects where id = object_id(N'up_VendorSetting_Insert') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure up_VendorSetting_Insert
GO

create proc up_VendorSetting_Insert
	 @Vendor_SettingId int =null OUTPUT  
	,@VendorFK int = null  
	,@Value text = null  
as
	/* 	Initial Generated Date:  Saturday, February 07, 2009 
		Purpose: Inserts a new record; missing arguments are set as their default (typically null)
	*/

SET NOCOUNT ON	
--int

insert tb_Vendor_Settings 
	(
	
	 [VendorFK] 
	,[Value] 
	)
	values
	(
	
	 @VendorFK 
	,@Value 
	)
set @Vendor_SettingId = scope_identity()
select * from v_tb_Vendor_Settings where Vendor_SettingId = @Vendor_SettingId

GO




--use PolePosition

if exists (select * from dbo.sysobjects where id = object_id(N'up_VendorSetting_Update') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure up_VendorSetting_Update
GO


create proc up_VendorSetting_Update
	 @Vendor_SettingId int  
	,@VendorFK int = null  
	,@Value text = null  
	,@OriginalVendor_SettingId int  = null --Original PrimaryKey
	
as
	/* 	Initial Generated Date:  Saturday, February 07, 2009 
		Purpose: Updates an existing record, verifying exist; missing arguments persist original record value
		NOTE: this version of the update allows up to write nulls to the table.
	*/

SET NOCOUNT ON	

if @OriginalVendor_SettingId is null
	set @OriginalVendor_SettingId = @Vendor_SettingId

-- test
declare @Result bit
execute @Result = up_VendorSetting_Test @OriginalVendor_SettingId
if (not @Result = 1)
begin
	Declare @x varchar(100)
	Set @x = Convert(Varchar(100), @Vendor_SettingId)
	raiserror(505060, 16, 1, 'VendorSetting', @x)
	return
end

-- proceed
update tb_Vendor_Settings 
set

	 [VendorFK] =  @VendorFK
	,[Value] =  @Value
	
from tb_Vendor_Settings source
where
	[source].[Vendor_SettingId] = @OriginalVendor_SettingId


select * from v_tb_Vendor_Settings where Vendor_SettingId = @Vendor_SettingId
GO





--use PolePosition

if exists (select * from dbo.sysobjects where id = object_id(N'up_VendorSetting_Delete') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure up_VendorSetting_Delete
GO


create proc up_VendorSetting_Delete
	@Vendor_SettingId int
as
	/* 	Initial Generated Date:  Saturday, February 07, 2009 
		Purpose: Deletes an existing record, verifying exist
	*/

SET NOCOUNT ON	

-- test
declare @Result bit
execute @Result = up_VendorSetting_Test @Vendor_SettingId
if (not @Result = 1)
begin
	Declare @x varchar(100)
	Set @x = Convert(Varchar(100), @Vendor_SettingId)
	raiserror(505060, 16, 1, 'VendorSetting', @x)
	return
end

-- proceed
delete from tb_Vendor_Settings 
where [Vendor_SettingId] = @Vendor_SettingId

GO


